package com.muyu.danmo.service.impl;

import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.LoginUser;
import com.muyu.danmo.domain.*;
import com.muyu.danmo.mapper.*;
import com.muyu.danmo.service.IDanmoTrainingInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.muyu.common.core.domain.Result.warn;

/**
 * 培训班详情Service业务层处理
 *
 * @author yangle
 * @date 2024-10-10
 */
@Service
public class DanmoTrainingInformationServiceImpl implements IDanmoTrainingInformationService {
    @Autowired
    private DanmoTrainingInformationMapper danmoTrainingInformationMapper;

    @Autowired
    private DanmoTrainingTeacherMapper danmoTrainingTeacherMapper;

    @Autowired
    private DanmoTrainingTeacherTypeMapper danmoTrainingTeacherTypeMapper;

    @Autowired
    private DanmoTrainingTeacherPictureMapper danmoTrainingTeacherPictureMapper;

    @Autowired
    private DanmoTrainingTypeMapper danmoTrainingTypeMapper;

    @Autowired
    private DanmoTariningInterorMapper danmoTariningInterorMapper;

    @Autowired
    private DanmoTrainingLikesMapper danmoTrainingLikesMapper;

    @Autowired
    private DanmoTrainingFavoriteMapper danmoTrainingFavoriteMapper;

    /**
     * 查询培训班详情
     *
     * @param id 培训班详情主键
     * @return 培训班详情
     */
    @Override
    public DanmoTrainingInformation selectDanmoTrainingInformationById(Long id) {
        DanmoTrainingInformation danmoTrainingInformation = danmoTrainingInformationMapper.selectDanmoTrainingInformationById(id);
        if (danmoTrainingInformation == null) {
            return null;
        }
        if (danmoTrainingInformation.getTrainingCourseLogo() == null) {
            danmoTrainingInformation.setTrainingCourseLogo(danmoTrainingInformation.getDoorList().get(0).getPhotosUrl());
        }
        danmoTrainingInformation.setViews(danmoTrainingInformation.getViews() + 1);
        updateDanmoTrainingInformationsetViews(danmoTrainingInformation);
        List<DanmoTrainingLikes> danmoTrainingLikes = danmoTrainingLikesMapper.selectDanmoTrainingLikesByIds(id);
        if (danmoTrainingLikes.size() != 0) {
            danmoTrainingInformation.setAmountPraise(Long.valueOf(danmoTrainingLikes.size()));
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();

        if (!danmoTrainingInformation.getUserId().equals(loginUser.getUserid())){
            danmoTrainingInformation.setBusinessLicense(null);
            danmoTrainingInformation.setFrontOfIdCard(null);
            danmoTrainingInformation.setReverseSideOfIdCard(null);
        }

        if (loginUser != null) {
            DanmoTrainingFavorite danmoTrainingFavorite = danmoTrainingFavoriteMapper.selectDanmoCollectMaterialByIdAndUserId(id, loginUser.getUserid());
            if (danmoTrainingFavorite != null) {
                danmoTrainingInformation.setIsCollect("true");
            }

            DanmoTrainingLikes danmoTrainingLikes1 = danmoTrainingLikesMapper.selectDanmoCollectMaterialByIdAndUserIdLiked(id, loginUser.getUserid());
            if (danmoTrainingLikes1 != null) {
                danmoTrainingInformation.setIsLike("true");
            }
        }
        return danmoTrainingInformation;
    }

    private void updateDanmoTrainingInformationsetViews(DanmoTrainingInformation danmoTrainingInformation) {

        danmoTrainingInformationMapper.updateDanmoTrainingInformationsetViews(danmoTrainingInformation);
    }

    /**
     * 查询培训班详情列表
     *
     * @param danmoTrainingInformation 培训班详情
     * @return 培训班详情
     */
    @Override
    public List<DanmoTrainingInformation> selectDanmoTrainingInformationList(DanmoTrainingInformation danmoTrainingInformation) {
        return danmoTrainingInformationMapper.selectDanmoTrainingInformationList(danmoTrainingInformation);
    }

    /**
     * 新增培训班详情
     *
     * @param danmoTrainingInformation 培训班详情
     * @return 结果
     */
    @Override
    public Result insertDanmoTrainingInformation(DanmoTrainingInformation danmoTrainingInformation) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return warn("请登录");
        }
        Long userid = loginUser.getUserid();
//        判断是否非空
        Result validationResult = validateDanmoTrainingInformation(danmoTrainingInformation);
        if (validationResult != null) {
            return validationResult;
        }

//        上线后需要改成2
        danmoTrainingInformation.setAuditStatus(1);
        danmoTrainingInformation.setCreateTime(DateUtils.getNowDate());
        danmoTrainingInformation.setUserId(userid);
        danmoTrainingInformationMapper.insertDanmoTrainingInformation(danmoTrainingInformation);

        Long danmoTrainingInformationId = danmoTrainingInformation.getId();

        // 第二步：为 DanmoPrintingDoor 和 DanmoPrintingWorks 和  InteriorPhotos等 设置 messageId
        setTrainingInformationIdForDoors(danmoTrainingInformation.getDoorList(), danmoTrainingInformationId);
        setTrainingInformationIdForWorks(danmoTrainingInformation.getDanmoTariningWorksList(), danmoTrainingInformationId);
        setTrainingInformationIdInteriorPhotos(danmoTrainingInformation.getDanmoTariningInterorList(), danmoTrainingInformationId);
        setTrainingTeacheForwards(danmoTrainingInformation.getDanmoTrainingTeacherList(), danmoTrainingInformationId);

        setTrainingTypes(danmoTrainingInformation.getTypeList(), danmoTrainingInformationId);

        // 第三步：插入其他相关数据
        insertDanmoDoorList(danmoTrainingInformation.getDoorList());
        insertDanmoTariningInterorList(danmoTrainingInformation.getDanmoTariningInterorList());
        insertDanmoTariningWorksList(danmoTrainingInformation.getDanmoTariningWorksList());
        insertDanmoTrainingTeacheForwards(danmoTrainingInformation.getDanmoTrainingTeacherList());
        insertDanmoTrainingTypes(danmoTrainingInformation.getTypeList());


        return Result.success(danmoTrainingInformation, "发布成功");
    }

    private void insertDanmoTrainingTypes(List<DanmoTrainingType> typeList) {
        if (typeList != null && !typeList.isEmpty()) {
            for (DanmoTrainingType danmoTrainingType : typeList) {
                danmoTrainingTypeMapper.insertDanmoTrainingType(danmoTrainingType);
            }
        }
    }

    private void setTrainingTypes(List<DanmoTrainingType> typeList, Long danmoTrainingInformationId) {
        if (typeList != null && !typeList.isEmpty()) {
            for (DanmoTrainingType danmoTrainingType : typeList) {
                danmoTrainingType.setDanmoTrainingInformationId(danmoTrainingInformationId);
            }
        }
    }

    private void insertDanmoTrainingTeacheForwards(List<DanmoTrainingTeacher> danmoTrainingTeacherList) {
        if (danmoTrainingTeacherList != null && !danmoTrainingTeacherList.isEmpty()) {
            for (DanmoTrainingTeacher danmoTrainingTeacher : danmoTrainingTeacherList) {
                danmoTrainingTeacherMapper.insertDanmoTrainingTeacher(danmoTrainingTeacher);
                Long danmoTrainingTeacherId = danmoTrainingTeacher.getId();

                // 第二步：为 DanmoTrainingTeacherPicture 和 DanmoTrainingTeacherType设置 danmoTrainingTeacherId
                setTrainingTeacherTypes(danmoTrainingTeacher.getDanmoTrainingTeacherTypeList(), danmoTrainingTeacherId);
                setTrainingTeacherPictures(danmoTrainingTeacher.getDanmoTrainingTeacherPictureList(), danmoTrainingTeacherId);

                // 第三步：插入其他相关数据
                insertDanmoTrainingTeacherTypeList(danmoTrainingTeacher.getDanmoTrainingTeacherTypeList());
                insertDanmoTrainingTeacherPictureList(danmoTrainingTeacher.getDanmoTrainingTeacherPictureList());
            }
        }
    }

    private void insertDanmoTrainingTeacherPictureList(List<DanmoTrainingTeacherPicture> danmoTrainingTeacherPictureList) {
        if (danmoTrainingTeacherPictureList != null && !danmoTrainingTeacherPictureList.isEmpty()) {
            for (DanmoTrainingTeacherPicture danmoTrainingTeacherPicture : danmoTrainingTeacherPictureList) {
                danmoTrainingTeacherPicture.setCreateTime(DateUtils.getNowDate());
                danmoTrainingTeacherPictureMapper.insertDanmoTrainingTeacherPicture(danmoTrainingTeacherPicture);
            }
        }
    }

    private void insertDanmoTrainingTeacherTypeList(List<DanmoTrainingTeacherType> danmoTrainingTeacherTypeList) {
        if (danmoTrainingTeacherTypeList != null && !danmoTrainingTeacherTypeList.isEmpty()) {
            for (DanmoTrainingTeacherType danmoTrainingTeacherType : danmoTrainingTeacherTypeList) {
                danmoTrainingTeacherTypeMapper.insertDanmoTrainingTeacherType(danmoTrainingTeacherType);
            }
        }
    }

    private void setTrainingTeacherPictures(List<DanmoTrainingTeacherPicture> danmoTrainingTeacherPictureList, Long danmoTrainingTeacherId) {
        if (danmoTrainingTeacherPictureList != null && !danmoTrainingTeacherPictureList.isEmpty()) {
            for (DanmoTrainingTeacherPicture danmoTrainingTeacherPicture : danmoTrainingTeacherPictureList) {
                danmoTrainingTeacherPicture.setDanmoTrainingTeacherId(danmoTrainingTeacherId);
            }
        }
    }

    private void setTrainingTeacherTypes(List<DanmoTrainingTeacherType> danmoTrainingTeacherTypeList, Long danmoTrainingTeacherId) {
        if (danmoTrainingTeacherTypeList != null && !danmoTrainingTeacherTypeList.isEmpty()) {
            for (DanmoTrainingTeacherType danmoTrainingTeacherType : danmoTrainingTeacherTypeList) {
                danmoTrainingTeacherType.setDanmoTrainingTeacherId(danmoTrainingTeacherId);
            }
        }
    }

    private void setTrainingTeacheForwards(List<DanmoTrainingTeacher> danmoTrainingTeacherList, Long danmoTrainingInformationId) {
        if (danmoTrainingTeacherList != null && !danmoTrainingTeacherList.isEmpty()) {
            for (DanmoTrainingTeacher danmoTrainingTeacher : danmoTrainingTeacherList) {
                danmoTrainingTeacher.setDanmoTrainingInformationId(danmoTrainingInformationId);
            }
        }
    }

    private Result validateDanmoTrainingInformation(DanmoTrainingInformation info) {

        if (info.getTitle() == null || info.getTitle().isEmpty()) {
            return Result.error("请输入标题");
        }
        if (info.getLocation() == null || info.getLocation().isEmpty()) {
            return Result.error("请输入授课地点");
        }
        if (info.getContent() == null || info.getContent().isEmpty()) {
            return Result.error("请输入培训内容概述");
        }
        if (info.getContactInfo() == null || info.getContactInfo().isEmpty()) {
            return Result.error("请输入联系方式");
        }
        if (info.getProvince() == null || info.getProvince().isEmpty() || info.getCitycode() == null || info.getCitycode().isEmpty() || info.getCountycode() == null || info.getCountycode().isEmpty()) {
            return Result.error("省市区不能为空");
        }
        if (info.getDanmoTrainingTeacherList().size() != 0) {
            for (DanmoTrainingTeacher danmoTrainingTeacher : info.getDanmoTrainingTeacherList()) {
                if (danmoTrainingTeacher.getName() == null || danmoTrainingTeacher.getName().isEmpty()) {
                    return Result.error("请输入老师姓名");
                }
                if (danmoTrainingTeacher.getPicture() == null || danmoTrainingTeacher.getPicture().isEmpty()) {
                    return Result.error("请上传老师头像");
                }
            }
        }

        return null;
    }

    private void insertDanmoTariningWorksList(List<DanmoTariningWorks> danmoTariningWorksList) {
        if (danmoTariningWorksList != null && !danmoTariningWorksList.isEmpty()) {
            danmoTrainingInformationMapper.insertDanmoTariningWorksList(danmoTariningWorksList);
        }
    }

    private void insertDanmoTariningInterorList(List<DanmoTariningInteror> danmoTariningInterorList) {
        if (danmoTariningInterorList != null && !danmoTariningInterorList.isEmpty()) {
            danmoTrainingInformationMapper.insertDanmoTariningInterorList(danmoTariningInterorList);
        }
    }

    private void insertDanmoDoorList(List<DanmoTariningDoor> doorList) {
        if (doorList != null && !doorList.isEmpty()) {
            danmoTrainingInformationMapper.insertDanmoTariningDoorList(doorList);
        }
    }

    private void setTrainingInformationIdInteriorPhotos(List<DanmoTariningInteror> danmoTariningInterorList, Long danmoTrainingInformationId) {
        if (danmoTariningInterorList != null && !danmoTariningInterorList.isEmpty()) {
            for (DanmoTariningInteror danmoTariningInteror : danmoTariningInterorList) {
                danmoTariningInteror.setInformationId(danmoTrainingInformationId);
            }
        }
    }

    private void setTrainingInformationIdForWorks(List<DanmoTariningWorks> danmoTariningWorksList, Long danmoTrainingInformationId) {
        if (danmoTariningWorksList != null && !danmoTariningWorksList.isEmpty()) {
            for (DanmoTariningWorks danmoTariningWorks : danmoTariningWorksList) {
                danmoTariningWorks.setInformationId(danmoTrainingInformationId);
            }
        }
    }

    private void setTrainingInformationIdForDoors(List<DanmoTariningDoor> doorList, Long danmoTrainingInformationId) {

        if (doorList != null && !doorList.isEmpty()) {
            for (DanmoTariningDoor door : doorList) {
                door.setInformationId(danmoTrainingInformationId);
            }
        }
    }

    /**
     * 修改培训班详情
     *
     * @param danmoTrainingInformation 培训班详情
     * @return 结果
     */
    @Override
    public Result updateDanmoTrainingInformation(DanmoTrainingInformation danmoTrainingInformation) {
        try {
            // 检查培训信息是否存在
            DanmoTrainingInformation existingTraining = danmoTrainingInformationMapper.selectById(danmoTrainingInformation.getId());
            if (existingTraining == null) {
                return Result.error("培训信息不存在");
            }
            Result result = validateDanmoTrainingInformation(danmoTrainingInformation);
            if (result != null) {
                return result;
            }

            deleteOldRelatedData(existingTraining.getId());

            // 更新相关的门、作品和室内照片等
            setTrainingInformationIdForDoors(danmoTrainingInformation.getDoorList(), danmoTrainingInformation.getId());
            setTrainingInformationIdForWorks(danmoTrainingInformation.getDanmoTariningWorksList(), danmoTrainingInformation.getId());
            setTrainingInformationIdInteriorPhotos(danmoTrainingInformation.getDanmoTariningInterorList(), danmoTrainingInformation.getId());
            setTrainingTeacheForwards(danmoTrainingInformation.getDanmoTrainingTeacherList(), danmoTrainingInformation.getId());
            setTrainingTypes(danmoTrainingInformation.getTypeList(), danmoTrainingInformation.getId());

            // 删除旧数据
            delTrainingInformationIdForDoors(danmoTrainingInformation.getDoorList());
            delTrainingInformationIdForWorks(danmoTrainingInformation.getDanmoTariningWorksList());
            delTrainingInformationIdInteriorPhotos(danmoTrainingInformation.getDanmoTariningInterorList());
            delTrainingTeacheForwards(danmoTrainingInformation.getDanmoTrainingTeacherList());
            delTrainingTypes(danmoTrainingInformation.getTypeList());


            // 第三步：插入其他相关数据
            insertDanmoDoorList(danmoTrainingInformation.getDoorList());
            insertDanmoTariningInterorList(danmoTrainingInformation.getDanmoTariningInterorList());
            insertDanmoTariningWorksList(danmoTrainingInformation.getDanmoTariningWorksList());
            insertDanmoTrainingTeacheForwards(danmoTrainingInformation.getDanmoTrainingTeacherList());
            insertDanmoTrainingTypes(danmoTrainingInformation.getTypeList());
            danmoTrainingInformationMapper.updateDanmoTrainingInformation(danmoTrainingInformation);
            return Result.success(danmoTrainingInformation, "修改成功");
        } catch (Exception e) {
            // 记录异常信息并返回错误
            return Result.error("修改失败: " + e.getMessage());
        }
    }

    private void delTrainingTypes(List<DanmoTrainingType> typeList) {
        if (typeList.size() != 0) {
            Long[] ids = new Long[typeList.size()];
            for (int i = 0; i < typeList.size(); i++) {
                ids[i] = typeList.get(i).getId();
            }
            danmoTrainingTypeMapper.deleteDanmoTrainingTypeByIds(ids);
        }
    }

    private void delTrainingTeacheForwards(List<DanmoTrainingTeacher> danmoTrainingTeacherList) {
        Long[] ids = new Long[danmoTrainingTeacherList.size()];
        for (int i = 0; i < danmoTrainingTeacherList.size(); i++) {
            ids[i] = danmoTrainingTeacherList.get(i).getId();
        }
        danmoTrainingTeacherMapper.deleteDanmoTrainingTeacherByIds(ids);
        danmoTrainingTeacherTypeMapper.deleteDanmoTrainingTeacherTypeByIdss(ids);
        danmoTrainingTeacherPictureMapper.deleteDanmoTrainingTeacherPictureByIdss(ids);
    }

    private void delTrainingInformationIdInteriorPhotos(List<DanmoTariningInteror> danmoTariningInterorList) {
        Long[] ids = new Long[danmoTariningInterorList.size()];
        for (int i = 0; i < danmoTariningInterorList.size(); i++) {
            ids[i] = danmoTariningInterorList.get(i).getId();
        }
        danmoTariningInterorMapper.deleteDanmoTariningInterorByIds(ids);
    }

    private void delTrainingInformationIdForWorks(List<DanmoTariningWorks> danmoTariningWorksList) {
        Long[] ids = new Long[danmoTariningWorksList.size()];
        for (int i = 0; i < danmoTariningWorksList.size(); i++) {
            ids[i] = danmoTariningWorksList.get(i).getId();
        }
        danmoTrainingTeacherPictureMapper.deleteDanmoTrainingTeacherPictureByIds(ids);
    }

    private void delTrainingInformationIdForDoors(List<DanmoTariningDoor> doorList) {
        Long[] ids = new Long[doorList.size()];
        for (int i = 0; i < doorList.size(); i++) {
            ids[i] = doorList.get(i).getId();
        }
        danmoTrainingInformationMapper.deleteDanmoTrainingInformationByIds(ids);
    }

    private void updateDanmoTariningWorksList(List<DanmoTariningWorks> danmoTariningWorksList) {
        if (danmoTariningWorksList != null && !danmoTariningWorksList.isEmpty()) {
            danmoTrainingInformationMapper.insertDanmoTariningWorksList(danmoTariningWorksList);
        }
    }

    private void updateDanmoTariningInterorList(List<DanmoTariningInteror> danmoTariningInterorList) {
        if (danmoTariningInterorList != null && !danmoTariningInterorList.isEmpty()) {
            danmoTrainingInformationMapper.insertDanmoTariningInterorList(danmoTariningInterorList);
        }
    }

    private void updateDanmoDoorList(List<DanmoTariningDoor> doorList) {
        if (doorList != null && !doorList.isEmpty()) {
            danmoTrainingInformationMapper.insertDanmoTariningDoorList(doorList);
        }
    }

    // 删除旧的门、作品和室内照片
    private void deleteOldRelatedData(Long trainingInformationId) {
        // 删除相关的门
        List<Long> oldDoorIds = danmoTrainingInformationMapper.selectIdsByTrainingDoorIds(trainingInformationId);
        if (!oldDoorIds.isEmpty()) {
            for (Long oldDoorId : oldDoorIds) {
                danmoTrainingInformationMapper.deleteDanmoTariningDoor(oldDoorId);
            }
        }

        // 删除相关的作品
        List<Long> oldWorkIds = danmoTrainingInformationMapper.selectIdsByTrainingWorkIds(trainingInformationId);
        if (!oldWorkIds.isEmpty()) {
            danmoTrainingInformationMapper.batchDeleteoldWorkIds(oldWorkIds);
        }

        // 删除相关的室内照片
        List<Long> oldInteriorPhotoIds = danmoTrainingInformationMapper.selectIdsByTrainingInteriorPhotoIds(trainingInformationId);
        if (!oldInteriorPhotoIds.isEmpty()) {
            danmoTrainingInformationMapper.batchDeleteOldInteriorPhotoIds(oldInteriorPhotoIds);
        }
    }

    /**
     * 批量删除培训班详情
     *
     * @param ids 需要删除的培训班详情主键
     * @return 结果
     */
    @Override
    public Result deleteDanmoTrainingInformationByIds(Long[] ids) {
        try {
            for (Long id : ids) {
                deleteOldRelated(id);
                danmoTrainingInformationMapper.deledanmoPrintingTypeRelation(id);
            }
            danmoTrainingInformationMapper.deleteDanmoTrainingInformationByIdss(ids);
            return Result.success(ids, "删除成功");
        } catch (Exception e) {
            return Result.error("删除失败");
        }
    }

    private void deleteOldRelated(Long id) {
        danmoTrainingInformationMapper.deleteDanmoTariningInteror(id);
        danmoTrainingInformationMapper.deleteDanmoTariningWorks(id);
        danmoTrainingInformationMapper.deleteDanmoTariningDoor(id);
        danmoTrainingLikesMapper.deleteDanmoTrainingLikesByIdss(id);

        danmoTrainingFavoriteMapper.deleteDanmoTrainingFavoriteById(id);
        List<DanmoTrainingTeacher> danmoTrainingTeacher = danmoTrainingTeacherMapper.selectDanmoTrainingTeacherByIds(id);
        if (danmoTrainingTeacher.size() != 0) {
            Long[] ids = new Long[danmoTrainingTeacher.size()];
            for (int i = 0; i < danmoTrainingTeacher.size(); i++) {
                ids[i] = danmoTrainingTeacher.get(i).getId();
            }
            danmoTrainingTeacherTypeMapper.deleteDanmoTrainingTeacherTypeByIdss(ids);
            danmoTrainingTeacherPictureMapper.deleteDanmoTrainingTeacherPictureByIdss(ids);
        }
        danmoTrainingTeacherMapper.deleteDanmoTrainingTeacherByIdss(id);
        danmoTrainingTypeMapper.deleteDanmoTrainingTypeByIdss(id);
    }

    /**
     * 删除培训班详情信息
     *
     * @param id 培训班详情主键
     * @return 结果
     */
    @Override
    public int deleteDanmoTrainingInformationById(Long id) {
        return danmoTrainingInformationMapper.deleteDanmoTrainingInformationById(id);
    }

    @Autowired
    private DanmoTrainingTypeRelationMapper danmoTrainingTypeRelationMapper;

    @Override
    public Result addDanmoTrainingInformation(DanmoTrainingInformation danmoTrainingInformation) {


        // 获取当前用户 ID
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userid = loginUser.getUserid();

        // 设置用户 ID（如果需要）
        if (userid != null) {
            danmoTrainingInformation.setUserId(userid);
        }

        try {

            DanmoTrainingInformation danmoTrainingInformation1 = new DanmoTrainingInformation();
            danmoTrainingInformation1.setUserId(userid);
            List<DanmoTrainingInformation> danmoTrainingInformations = danmoTrainingInformationMapper.selectDanmoTrainingInformationList(danmoTrainingInformation1);
            if (danmoTrainingInformations.size() > 0) {
                return Result.error("您已发布过培训信息，请勿重复发布");
            }
            danmoTrainingInformation.setStatus(Long.valueOf(0));
            danmoTrainingInformation.setViews(Long.valueOf(0));
            danmoTrainingInformation.setCreateTime(DateUtils.getNowDate());
            danmoTrainingInformation.setAmountPraise(Long.valueOf(0));
//            // 添加培训信息
//            danmoTrainingInformationMapper.addDanmoTrainingInformation(danmoTrainingInformation);
//
//            // 处理培训类型关系
//            List<DanmoTrainingTypeRelation> relations = new ArrayList<>();
//            for (DanmoTrainingType danmoTrainingType : danmoTrainingInformation.getTypeList()) {
//                DanmoTrainingTypeRelation relation = new DanmoTrainingTypeRelation();
//                relation.setTrainingInformationId(danmoTrainingInformation.getId());
//                relation.setTrainingTypeId(danmoTrainingType.getId());
//                relations.add(relation);
//            }
//            // 批量插入
//            danmoTrainingTypeRelationMapper.batchInsert(relations);

            Long danmoTrainingInformationId = danmoTrainingInformation.getId();

            // 第二步：为 DanmoPrintingDoor 和 DanmoPrintingWorks 和  InteriorPhotos 设置 messageId
            setTrainingInformationIdForDoors(danmoTrainingInformation.getDoorList(), danmoTrainingInformationId);
            setTrainingInformationIdForWorks(danmoTrainingInformation.getDanmoTariningWorksList(), danmoTrainingInformationId);
            setTrainingInformationIdInteriorPhotos(danmoTrainingInformation.getDanmoTariningInterorList(), danmoTrainingInformationId);

            // 第三步：插入其他相关数据
            insertDanmoDoorList(danmoTrainingInformation.getDoorList());
            insertDanmoTariningInterorList(danmoTrainingInformation.getDanmoTariningInterorList());
            insertDanmoTariningWorksList(danmoTrainingInformation.getDanmoTariningWorksList());
//            // 处理培训信息图片
//            List<DanmoTrainingInformationPicture> danmoTrainingInformationPictureList = danmoTrainingInformation.getDanmoTrainingInformationPictureList();
//            for (DanmoTrainingInformationPicture danmoTrainingInformationPicture : danmoTrainingInformationPictureList) {
//                danmoTrainingInformationPicture.setTrainingInformationId(danmoTrainingInformation.getId());
//                danmoTrainingInformationPictureMapper.insertDanmoTrainingInformationPicture(danmoTrainingInformationPicture);
//            }
            return Result.success(danmoTrainingInformation, "添加成功");
        } catch (Exception e) {
            return Result.error("添加失败: " + e.getMessage());
        }
    }


    @Override
    public List<DanmoTrainingInformation> selectDanmoTrainingInformationByUserId(Long userId) {
        DanmoTrainingInformation danmoTrainingInformation = new DanmoTrainingInformation();
        danmoTrainingInformation.setUserId(userId);
        return danmoTrainingInformationMapper.selectDanmoTrainingInformationByUserId(danmoTrainingInformation);
    }

    @Override
    public List<DanmoTrainingInformation> selectDanmoTrainingInformationViewsList(DanmoTrainingInformation danmoTrainingInformation) {
        return danmoTrainingInformationMapper.selectDanmoTrainingInformationViewsList(danmoTrainingInformation);
    }

    @Override
    public List<DanmoTrainingInformation> selectDanmoTrainingInformationAmountPraiseList(DanmoTrainingInformation danmoTrainingInformation) {
        List<DanmoTrainingInformation> danmoTrainingInformations = danmoTrainingInformationMapper.selectDanmoTrainingInformationAmountPraiseList(danmoTrainingInformation);
        for (DanmoTrainingInformation trainingInformation : danmoTrainingInformations) {
            List<DanmoTrainingLikes> danmoTrainingLikes = danmoTrainingLikesMapper.selectDanmoTrainingLikesByIds(trainingInformation.getId());
            if (danmoTrainingLikes.size() != 0) {
                trainingInformation.setAmountPraise(Long.valueOf(danmoTrainingLikes.size()));
            }
        }
        return danmoTrainingInformations;
    }
}